<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4
 * Time: 0:04
 */

class Finance_model extends MY_Model
{
    private $table = 'finance';
    public function __construct()
    {
        parent::__construct($this->table);
    }

    public function getList($companyId)
    {
        $data['company_id'] = $companyId;
        $data['deleteFlag'] = 0;

        $this->db->where_in('status', array(0,1));
        $this->db->select('id, phone, account, user_name, status, create_time, update_time');
        $this->_get_db($data);
        $info['data'] = $this->db->get('finance')->result_array();
        foreach ($info['data'] as &$value){
            if ($value['create_time'] != '0') {
                $value['create_time'] = date('Y-m-d  H:i:s', $value['create_time']);
            }
            if ($value['update_time'] != '0') {
                $value['update_time'] = date('Y-m-d  H:i:s', $value['update_time']);
            }else{
                $value['update_time'] = '未更新';
            }

            if ($value['status'] == 0) {
                $value['status'] = '账户正常';
            }else if($value['status'] == 1){
                $value['status'] = '审核中';
            }else if($value['status'] == 2){
                $value['status'] = '审核失败';
            }
        }
        return $info;
    }

    public function getAll()
    {
        $data['finance.deleteFlag'] = 0;

        if ($this->input->get('phone', TRUE)) {
            $data['finance.phone'] = $this->input->get('phone', TRUE);
        }
        if ($this->input->get('account', TRUE)) {
            $this->db->like('finance.account', $this->input->get('account', TRUE));
        }
        if ($this->input->get('user_name', TRUE)) {
            $this->db->like('finance.user_name', $this->input->get('user_name', TRUE));
        }
        $this->db->where_in('finance.status', array(0,1));
//        $this->_join('company', 'company.companyId = finance.company_id', 'left');
        $this->_join('admin', 'admin.adminId = finance.operator', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count');
        $info = $this->db->get('finance')->row_array();

        if ($this->input->get('phone', TRUE)) {
            $data['finance.phone'] = $this->input->get('phone', TRUE);
        }
        if ($this->input->get('account', TRUE)) {
            $this->db->like('finance.account', $this->input->get('account', TRUE));
        }
        if ($this->input->get('user_name', TRUE)) {
            $this->db->like('finance.user_name', $this->input->get('user_name', TRUE));
        }
        $this->db->where_in('finance.status', array(0,1));
        $this->db->select('finance.id, finance.phone, finance.account, finance.user_name, finance.status,'
                        . 'finance.create_time, finance.update_time, finance.company_id, adminName, type');
        $this->_get_db($data);
        $info['data'] = $this->db->get('finance')->result_array();
        foreach ($info['data'] as &$value){
            if ($value['create_time'] != '0') {
                $value['create_time'] = date('Y-m-d  H:i:s', $value['create_time']);
            }
            if ($value['update_time'] != '0') {
                $value['update_time'] = date('Y-m-d  H:i:s', $value['update_time']);
            }else{
                $value['update_time'] = '未更新';
            }

            if ($value['status'] == 0) {
                $value['status'] = '账户正常';
            }else if($value['status'] == 1){
                $value['status'] = '审核中';
            }else if($value['status'] == 2){
                $value['status'] = '审核失败';
            }
            if($value['type'] == 1) {
                $this->db->where('companyId', $value['company_id']);
                $this->db->select('name');
                $shop = $this->db->get('company')->row_array();
                $value['name'] = $shop['name'];
            } else if($value['type'] == 2) {
                $value['name'] = '总公司';
            }
        }
        return $info;
    }

    public function add($data)
    {
        $this->db->insert('finance',$data);
        return $this->db->insert_id();
    }

    public function edit($id, $info)
    {
        $this->db->where('id',$id);
        $this->db->update('finance',$info);
        return $this->db->affected_rows();
    }

    public function getOne($data)
    {
        $this->db->where($data);
        return $this->db->get('finance')->row_array();
    }

    public function del($id)
    {
        $this->db->where('id',$id);
        $this->db->set('deleteFlag',1);
        $this->db->update('finance');
        return $this->db->affected_rows();
    }

    public function check($id, $operator)
    {
        $this->db->where('id',$id);
        $this->db->set('status',0);
        $this->db->set('operator',$operator);
        $this->db->update('finance');
        return $this->db->affected_rows();
    }

    public function checkno($id, $operator)
    {
        $this->db->where('id',$id);
        $this->db->set('status',2);
        $this->db->set('operator',$operator);
        $this->db->update('finance');
        return $this->db->affected_rows();
    }
}